<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>4-5</title>
	<style type="text/css">
		pre {
			color: green;
			border: 1px dotted black;
			padding-left: 20px;
			background: #EDEDED;
		}
		input {
			float: left;
			margin-left: 10px;
		}
		input:nth-of-type(1) {
			margin-left: 520px;
		}
	</style>
	<script type="text/javascript">
		// 封装
		var EventUtil = {
			addHandler: function(oElement, sEvent, fnHandler) {
				oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on"+sEvent, fnHandler)
			},
			removeHandler: function (oElement, sEvent, fnHandler) {
				oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler)
			},
			// 给window的load事件添加函数，其实相当于window.onload = function(){} ?
			addLoadHandler: function (fnHandler) {
				this.addHandler(window, "load", fnHandler)
			}
		}
		EventUtil.addLoadHandler(function(){
			var input = document.getElementsByTagName("input");
			// 其实有点像事件里面还有事件的感觉
			EventUtil.addHandler(input[1],"click",function(){
				EventUtil.addHandler(input[0],"click",fnHandler);
				input[0].value="我可以点击了";
			});

			EventUtil.addHandler(input[2],"click",function(){
				EventUtil.removeHandler(input[0],"click",fnHandler);
				input[0].value="毫无用处的按钮";
			});

			function fnHandler() {
				alert("事件绑定成功！");
			}
		});
	</script>
</head>
<body>
<pre>

&lt;script type="text/javascript"&gt;
var EventUtil = {
	addHandler: function (oElement, sEvent, fnHandler) {
		oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler)
	},
	removeHandler: function (oElement, sEvent, fnHandler) {
		oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler)
	},
	addLoadHandler: function (fnHandler) {
		this.addHandler(window, "load", fnHandler)
	}
}
&lt;/script&gt;
	</pre>
<input type="button" value="毫无用处的按钮">
<input type="button" value="绑定click">
<input type="button" value="解除绑定">
<script type="text/javascript" src="8_event.js"></script>
</body>
</html>
